/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.basedata.college.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.common.site.modules.sys.utils.UserUtils;
import com.smooth.dao.gen.BaseCategoryMapper;
import com.smooth.dao.gen.BaseCollegeMapper;
import com.smooth.dao.gen.BaseProvincesMapper;
import com.smooth.dao.gen.bean.BaseCategory;
import com.smooth.dao.gen.bean.BaseCollege;
import com.smooth.dao.gen.bean.BaseCollegeExample;
import com.smooth.dao.gen.bean.BaseProvinces;
import com.smooth.dao.genx.GxCollegeMapper;
import com.smooth.site.sx.mag.basedata.category.service.BaseCategoryService;
import com.smooth.site.sx.pc.base.enums.OnlineStatus;
import com.smooth.site.sx.pc.base.req.QueryCollegeReq;

/**
 * 高校Service
 * @author 北冥
 * @version 2018-12-31
 */
@Service
@Transactional(readOnly = true)
public class BaseCollegeService{

	@Autowired
	BaseCollegeMapper baseCollegeMapper;
	@Autowired
	GxCollegeMapper gxCollegeMapper;
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	BaseCategoryMapper baseCategoryMapper;
	@Autowired
	BaseProvincesMapper baseProvincesMapper;
	@Autowired
	BaseCategoryService baseCategoryService;
	public BaseCollege get(String no) {
		return baseCollegeMapper.selectByPrimaryKey(no);
	}
	
	public List<BaseCollege> findList(BaseCollege baseCollege) {
		BaseCollegeExample baseCollegeExample = new BaseCollegeExample();
		BaseCollegeExample.Criteria criteria = baseCollegeExample.createCriteria();
		setQuery(criteria,baseCollege);
		return baseCollegeMapper.selectByExample(baseCollegeExample);
	}
	public void setCategory(BaseCollege baseCollege) {
		if(StringUtils.isNotBlank(baseCollege.getCategoryId())) {
			BaseCategory baseCategory = baseCategoryMapper.selectByPrimaryKey(baseCollege.getCategoryId());
			
			baseCollege.setCategoryName(baseCategory.getCategoryName());
		}
		if(StringUtils.isNotBlank(baseCollege.getProvinceId())) {
			BaseProvinces baseProvinces = baseProvincesMapper.selectByPrimaryKey(baseCollege.getProvinceId());
			baseCollege.setProvinceName(baseProvinces.getProvinceName());
		}
	}
	private void setQuery(BaseCollegeExample.Criteria criteria ,BaseCollege baseCollege) {
		if(StringUtils.isNotBlank(baseCollege.getCategoryId())) {
			criteria.andCategoryIdEqualTo(baseCollege.getCategoryId());
		}
		if(StringUtils.isNotBlank(baseCollege.getCollegeCode())) {
			criteria.andCollegeCodeEqualTo(baseCollege.getCollegeCode());
		}
		
		if(StringUtils.isNotBlank(baseCollege.getProvinceId())) {
			criteria.andProvinceIdEqualTo(baseCollege.getProvinceId());
		}
		
		if(StringUtils.isNotBlank(baseCollege.getLevel())) {
			criteria.andLevelEqualTo(baseCollege.getLevel());
		}
		
		if(UserUtils.isSchMag()) {
			criteria.andIdEqualTo(UserUtils.getUser().getSchId());
		}
	}
	public Page<BaseCollege> findPage(Page<BaseCollege> page, BaseCollege baseCollege) {
		BaseCollegeExample baseCollegeExample = new BaseCollegeExample();
		BaseCollegeExample.Criteria criteria = baseCollegeExample.createCriteria();
		
		setQuery(criteria,baseCollege);
		baseCollegeExample.setPage(page);
		
		List<BaseCollege> dlist = baseCollegeMapper.selectByExample(baseCollegeExample);
		page.setList(dlist);
		page.setCount(baseCollegeMapper.countByExample(baseCollegeExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(BaseCollege baseCollege) {
		BaseCategory baseCategory = baseCategoryService.get(baseCollege.getCategoryId());
		if(baseCategory!=null) {
			baseCollege.setCategoryName(baseCategory.getCategoryName());
		}
		if(StringUtils.isNotEmpty(baseCollege.getId())&&get(baseCollege.getId())!=null) {
			baseCollegeMapper.updateByPrimaryKeySelective(baseCollege);
		}else {
			Date date = new Date();
			baseCollege.setCreateDate(date);
			baseCollege.setId(idGenerater.genClsNo("baseCollege"));
			baseCollege.setUpdateDate(date);
			baseCollegeMapper.insertSelective(baseCollege);
		}
	}
	
	
	@Transactional(readOnly = false)
	public void delete(BaseCollege baseCollege) {
		baseCollegeMapper.deleteByPrimaryKey(baseCollege.getId());
	}
	
	
	public List<BaseCollege> queryCollege(QueryCollegeReq queryCollegeReq){
		
		List<BaseCollege> blist = gxCollegeMapper.queryCollegeByStr(queryCollegeReq.getProvinceId(), queryCollegeReq.getCollegeName(), queryCollegeReq.getQueryStr());
		return blist;
	}
	
}